//
//  ViewController.m
//  TomCat
//
//  Created by jikang.wu on 2021/7/20.
//

#import "ViewController.h";
#import <AudioToolbox/AudioToolbox.h>

@interface ViewController ()

@property (weak, nonatomic) IBOutlet UIImageView *imgViewCat;
- (IBAction)doAction:(UIButton *)sender;
- (void)startAnimation:(int)count playPicName:(NSString *) playPicName soundName:(NSString *) soundName;
- (void)playSoundEffect:(NSString *)name;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

/**
 * 播放音效文件
 *
 * @param name 音频文件名称 */
-(void)playSoundEffect:(NSString *)name{
    NSString *audioFile=[[NSBundle mainBundle] pathForResource:name ofType:nil];
    NSURL *fileUrl=[NSURL fileURLWithPath:audioFile];
    //1.获得系统声音ID
    SystemSoundID soundID=0;
    /**
     * inFileUrl: 音频文件url
     * outSystemSoundID:声 id(此函数会将音效文件加入到系统音频服务中并返回一个长整形ID) */
    AudioServicesCreateSystemSoundID((__bridge CFURLRef)(fileUrl), &soundID);
    //如果需要在播放完之后执行某些操作,可以调用如下方法注册一个播放完成回调函数 AudioServicesAddSystemSoundCompletion(soundID, NULL, NULL, soundCompleteCallback, NULL);
    //2.播放音频
    AudioServicesPlaySystemSound(soundID);//播放音效
    AudioServicesPlayAlertSound(soundID);//播放音效并震动
}

// 实现方法
// 其中使用到三个参数，分别是count，playPicName，soundName
- (void)startAnimation:(int)count playPicName:(NSString *) playPicName soundName:(NSString *) soundName  {
    NSLog(@"%@", @"开始动画");
    // 通过isAnimating属性进行判断，当前动画是否正在播放，当正在播放时，阻止其重新开始播放
    if(self.imgViewCat.isAnimating){
        return;
    }
    // 定义数组，用于存储需要播放动画的图片资源
    NSMutableArray *arrayM = [NSMutableArray array];
    for (int i = 0; i <= count; i++) {
    // 按照索引进行格式化图片名称，【@"%@_%02d.jpg"】格式用来格式化名称，同时补齐数字
        NSString *imgName = [NSString stringWithFormat: @"%@_%02d.jpg", playPicName, i];
        // [NSBundle mainBundle]表示安装包所在的地址路径
        // 封装资源图片的路径地址
        NSString *path = [[NSBundle mainBundle] pathForResource:imgName ofType:nil];
        // 按照路径读取图片资源文件
        UIImage *imgCat = [UIImage imageWithContentsOfFile: path];
        NSLog(@"%@_%@_%@", imgCat, path, imgName);
        [arrayM addObject:imgCat];
        
    }
    
    self.imgViewCat.animationImages = arrayM;
    // 设置每一帧之间的间隔
    self.imgViewCat.animationDuration = count * 0.04;
    // 设置动画重复次数
    self.imgViewCat.animationRepeatCount = 1;
    // 同步调用音频播放，在后面一步进行实现该方法
    [self playSoundEffect: soundName];
    // 开始播放动画
    [self.imgViewCat startAnimating];
    
}



- (IBAction)doAction:(UIButton *)sender {
    NSLog(@"%@", @"6666");
    switch (sender.tag) {
        case 10:
            NSLog(@"%@", @"10");
            [self startAnimation: 12 playPicName:@"cymbal" soundName: @"cymbal.m4a"];
            break;
        case 20:
            NSLog(@"%@", @"20");
            [self startAnimation: 27 playPicName:@"fart" soundName: @"fart003_11025.m4a"];
            break;
           
        case 30:
            NSLog(@"%@", @"30");
            [self startAnimation: 80 playPicName:@"drink" soundName: @"drink.m4a"];
            break;
           
        case 40:
            NSLog(@"%@", @"40");
            [self startAnimation: 39 playPicName:@"eat" soundName: @"eat.m4a"];
            break;
        case 50:
            NSLog(@"%@", @"50");
            [self startAnimation: 55 playPicName:@"scratch" soundName: @"scratch.m4a"];
            break;
           
        case 60:
            NSLog(@"%@", @"60");
            [self startAnimation: 23 playPicName:@"pie" soundName: @"pie.m4a"];
            break;
            
        case 70:
            NSLog(@"%@", @"70");
            [self startAnimation: 80 playPicName:@"knockout" soundName: @"knockout.m4a"];
            break;
        case 80:
            NSLog(@"%@", @"80");
            [self startAnimation: 29 playPicName:@"foot_right" soundName: @"foot_right.m4a"];
            break;
        case 90:
            NSLog(@"%@", @"90");
            [self startAnimation: 29 playPicName:@"foot_left" soundName: @"foot_left.m4a"];
            break;
           
        default:
            break;
    }
}

@end
